package com.lanyuan.controller.sys;

import com.lanyuan.annotation.SystemLog;
import com.lanyuan.controller.index.BaseController;
import com.lanyuan.entity.sys.SysContentFormMap;
import com.lanyuan.exception.SystemException;
import com.lanyuan.mapper.sys.SysContentMapper;
import com.lanyuan.plugin.PageView;
import com.lanyuan.util.Common;
import com.lanyuan.util.DateUtil;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.inject.Inject;

@Controller
@RequestMapping("/sys/content/")
public class SysContentController extends BaseController {

    @Inject
    private SysContentMapper sysContentMapper;

    @RequestMapping("list")
    public String listUI(Model model) throws Exception {
        model.addAttribute("res", findByRes());
        return Common.BACKGROUND_PATH + "/sys/content/list";
    }

    @ResponseBody
    @RequestMapping("findByPage")
    public PageView findByPage(String pageNow, String pageSize, String column, String sort) throws Exception {
        SysContentFormMap formMap = getFormMap(SysContentFormMap.class);
        formMap = toFormMap(formMap, pageNow, pageSize, formMap.getStr("orderby"));
        formMap.put("column", column);
        formMap.put("sort", sort);
        PageView pageView = getPageView(formMap);
        pageView.setRecords(sysContentMapper.findPage(formMap)); // 不调用默认分页,调用自已的mapper中findPage
        return pageView;
    }

    @RequestMapping("addUI")
    public String addUI(Model model) throws Exception {
        return Common.BACKGROUND_PATH + "/sys/content/add";
    }

    @ResponseBody
    @RequestMapping("addEntity")
    @SystemLog(module = "后台管理", methods = "内容管理-新增内容") // 凡需要处理业务逻辑的.都需要记录操作日志
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    public String addEntity() {
        try {
            SysContentFormMap formMap = getFormMap(SysContentFormMap.class);
            formMap.set("created_on", DateUtil.formatDateTime());
            formMap.set("is_hidden", "0");
            sysContentMapper.addEntity(formMap); // 新增后返回新增信息
        } catch (Exception e) {
            throw new SystemException("添加内容异常");
        }
        return "success";
    }

    @RequestMapping("editUI")
    public String editUI(Model model) {
        String id = getPara("id");
        if (Common.isNotEmpty(id)) {
            SysContentFormMap formMap = sysContentMapper.findOne("id", id, SysContentFormMap.class);
            model.addAttribute("sysContent", formMap);
        }
        return Common.BACKGROUND_PATH + "/sys/content/edit";
    }

    @ResponseBody
    @RequestMapping("editEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "系统管理", methods = "内容管理-修改内容信息") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editEntity() throws Exception {
        String code = "success";
        SysContentFormMap formMap = getFormMap(SysContentFormMap.class, false);
        sysContentMapper.editEntity(formMap);
        return code;
    }

    @ResponseBody
    @RequestMapping("deleteEntity")
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "系统管理", methods = "内容管理-删除内容") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String deleteEntity() throws Exception {
        String[] ids = getParaValues("ids");
        for (String id : ids) {
            sysContentMapper.deleteByAttribute("id", id, SysContentFormMap.class);
        }
        return "success";
    }

    @RequestMapping("editIsHidden")
    @ResponseBody
    @Transactional(readOnly = false) // 需要事务操作必须加入此注解
    @SystemLog(module = "系统管理", methods = "内容管理-修改是否隐藏状态") // 凡需要处理业务逻辑的.都需要记录操作日志
    public String editIsHidden() throws Exception {
        String id = getPara("id");
        String status = getPara("status");
        SysContentFormMap formMap = getFormMap(SysContentFormMap.class);
        formMap.set("id", id);
        formMap.set("is_hidden", status);
        sysContentMapper.editEntity(formMap);
        return "success";
    }

}
